﻿using System;
using System.Configuration;
using System.Data;
using System.Net.Mail;
using System.Threading;
using Talentnet.Core.Utilities;
using Talentnet.Core.Utilities.Mail;

namespace TLN.WS
{
    public class TalentnetBusiness
    {
        private log4net.ILog _logFirst = log4net.LogManager.GetLogger("FirstLogger");
        private log4net.ILog _logSecond = log4net.LogManager.GetLogger("SecondLogger");

        public TalentnetBusiness() { }

        private MailFormat GetMailFormat(int type)
        {
            switch (type)
            {
                case 0:
                    return MailFormat.Text; ;
                case 1:
                    return MailFormat.Html;
                default:
                    return MailFormat.Text;
            }
        }

        private MailPriority GetMailPriority(int priority)
        {
            switch (priority)
            {
                case 0:
                    return MailPriority.Normal;
                case 1:
                    return MailPriority.Low;
                case 2:
                    return MailPriority.High;
                default:
                    return MailPriority.Normal;
            }
        }


        /// <summary>
        /// Send email
        /// </summary>
        public void SendEmail(MailServiceSettings _mailServiceSettings)
        {
            try
            {
                DataTable emailList = new DataTable();
                Int64 recordID = 0;
                string emailFrom = string.Empty;
                string emailTo = string.Empty;
                string emailCC = string.Empty;
                string emailBCC = string.Empty;
                string emailSubject = string.Empty;
                string emailBody = string.Empty;
                bool emailAttachment = false;
                string emailAttachmentFiles = string.Empty;
                MailFormat emailFormat = MailFormat.Text;//0 = Text; 1 = HTML
                MailPriority emailPriority = MailPriority.Normal;//0 = Normal; 1 = Low; 2 = High
                MailServiceSettings emailSetting = _mailServiceSettings;
                bool isSuccess = true;
                int failedAttempt = 0;
                foreach (DataRow email in emailList.Rows)
                {
                    recordID = Convert.ToInt64(email["RecordID"]);
                    emailFrom = email["From"].ToString();
                    emailTo = email["To"].ToString();
                    emailCC = email["CC"].ToString();
                    emailBCC = email["BCC"].ToString();
                    emailSubject = email["Subject"].ToString();
                    emailBody = email["Body"].ToString();
                    emailFormat = GetMailFormat(Convert.ToInt16(email["FormatType"]));//0 = Text; 1 = HTML
                    emailPriority = GetMailPriority(Convert.ToInt16(email["Priority"]));//0 = Normal; 1 = Low; 2 = High
                    emailAttachment = Convert.ToBoolean(email["Attachment"]);
                    emailAttachmentFiles = "";
                    if (emailAttachment)
                    {
                        if (email["AttachmentFile1"].ToString().Trim() != "")
                        {
                            emailAttachmentFiles += email["AttachmentFile1"].ToString() + ";";
                        }
                        if (email["AttachmentFile2"].ToString().Trim() != "")
                        {
                            emailAttachmentFiles += email["AttachmentFile2"].ToString() + ";";
                        }
                        if (email["AttachmentFile3"].ToString().Trim() != "")
                        {
                            emailAttachmentFiles += email["AttachmentFile3"].ToString() + ";";
                        }
                    }
                    emailAttachmentFiles = emailAttachmentFiles.TrimEnd(';');

                    try
                    {
                        //isSuccess = MailService.SendEmail(emailFrom, emailTo, emailCC, emailBCC, emailSubject, emailBody, emailAttachmentFiles, emailFormat, emailPriority, emailSetting);
                        if (!isSuccess)
                        {
                            failedAttempt = Convert.ToInt32(email["FailedAttempt"]);
                            failedAttempt++;
                        }
                    }
                    catch (Exception ex)//Fail
                    {
                        failedAttempt = Convert.ToInt32(email["FailedAttempt"]);
                        failedAttempt++;
                        isSuccess = false;
                        LogManager.Log.Error(ex);
                    }

                    //Update to Database
                    
                }

            }
            catch (Exception ex)
            {
                log4net.ILog logger = log4net.LogManager.GetLogger("TalentnetServices");
                logger.Error(ex);
            }

        }

        public void SendEmailTest(MailServiceSettings mailServiceSettings)
        {
            try
            {
                string emailFrom = "admin@talentnet.vn";
                string emailTo = "phuchua@gmail.com";
                string emailCC = string.Empty;
                string emailBCC = string.Empty;
                string emailSubject = "Subject test";
                string emailBody = "This is a body";

                MailServices.SendEmail(emailFrom, emailTo, emailSubject, emailBody, mailServiceSettings);
                _logFirst.Info("Send email: Done");
            }
            catch (Exception ex)
            {
                _logFirst.Error(ex);
            }
        }
    }
}
